<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-tickets"></i> 菜单列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">

            <div class="handle-box">
                <el-button type="primary" icon="delete" class="handle-del mr10" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
                <el-input v-model="name" placeholder="菜单名"  class="handle-input mr10"></el-input>
                <el-input v-model="url" placeholder="访问路径"  class="handle-input mr10" ></el-input>
                <el-button type="primary" icon="search" @click="search">搜索</el-button>
                <el-button type="primary" @click="handleAdd">新增</el-button>
            </div>

            <!--列表-->
            <el-table :data="tableData" highlight-current-row   max-height="700" v-loading="listLoading" @selection-change="selsChange"
                      style="width: 100%;">
                <el-table-column type="selection" width="55">
                </el-table-column>
                <el-table-column
                    prop="name"
                    label="菜单名称"
                >
                </el-table-column>
                <el-table-column
                    prop="url"
                    label="访问地址"
                >
                </el-table-column>
                <el-table-column
                    prop="isChildren"
                    label="是否为子菜单"
                    :formatter="function(row,column) {
					  var a=row.isChildren
					  if(a==1){
						return '是'
					  }else{
						return '否'
					  }

					}"
                >
                </el-table-column>
                <el-table-column
                    prop="parentName"
                    label="父菜单">
                </el-table-column>
                <el-table-column
                    prop="createTime"
                    label="创建时间"
                    :formatter="function(row,column) {
         var date= new Date(row.createTime);
         var year=date.getFullYear();
         var  month=date.getMonth()+1;
         if(month<10){
          month='0'+month;
       }
		  var day=date.getDate();
		  if(day<10){
		  day='0'+day;
		  }
		  var hour= date.getHours();
		  if(hour<10){
		  hour='0'+hour;
		  }
		  var m= date.getMinutes()
		  if(m<10){
		  m='0'+m;
		  }
		  var s= date.getSeconds()
		  if(s<10){
		  s='0'+s;
		  }
		  return year+'-'+month+'-'+day+' '+hour+':'+m+':'+s;

			}"
                >
                </el-table-column>
                <el-table-column
                    prop="deleteFlag"
                    label="状态"
                    :formatter="function(row,column) {
          var a=row.deleteFlag
          if(a==1){
            return '启用'
          }else{
            return '删除'
          }

        }"
                >
                </el-table-column>

                <el-table-column label="操作" width="150">
                    <template scope="scope">
                        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>


            <div class="pagination">
                <el-pagination
                    background
                    layout="total, sizes, prev, pager, next, jumper"
                    :page-size="pageSize"
                    :current-page="pageNo"
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange" :total="total" style="float:right;">
                </el-pagination>
            </div>
        </div>



        <!--新增界面-->
        <el-dialog title="添加菜单"  :visible.sync="addFormVisible"   :before-close="addFromClose" >
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm" >
                <el-form-item label="菜单名称" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="访问路径" prop="url">
                    <el-input v-model="addForm.url" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="是否为子菜单">
                    <el-select v-model="addForm.isChildren" placeholder="请选择">
                        <el-option
                            label="是"
                            value="1">
                        </el-option>
                        <el-option
                            label="不是"
                            value="0">
                        </el-option>
                    </el-select>

                </el-form-item>

                <el-form-item label="父菜单">
                    <el-select v-model="addForm.parent" placeholder="请选择">
                        <el-option
                            label="是"
                            value="1">
                        </el-option>
                        <el-option
                            label="不是"
                            value="0">
                        </el-option>
                    </el-select>

                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFromClose">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
    import axiosHttpUtil from "../service/axiosHttpUtil.js";
    import serveceApi from "../service/serveceApi.js";


    export default {
        data() {
            return {
                name: '',
                url: '',
                tableData: [],
                total: 0,
                pageSize: 10,
                pageNo: 1,
                listLoading: true,
                sels: [],//列表选中列
                nameFlag:true,
                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    name: [
                        {required: true, message: '请输入姓名', trigger: 'blur'}
                    ]
                },
                //编辑界面数据
                editForm: {
                    id: 0,
                    name: '',
                    sex: -1,
                    age: 0,
                    birth: '',
                    addr: ''
                },

                addFormVisible: false,//新增界面是否显示
                addLoading: false,
                addFormRules: {
                    name: [
                        {required: true, message: '请输入菜单名称', trigger: 'blur'}
                    ],
                    url: [
                        {required: true, message: '请输入访问地址', trigger: 'blur'}
                    ]
                },
                //新增界面数据
                addForm: {
                    name: '',
                    url: '',
                    parent: ''
                }

            }
        },
        created() {
            this.getLogList(this.pageNo, this.pageSize);
        },
        methods: {
            //性别显示转换
            formatSex: function (row, column) {
                return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
            },
            //获取用户列表
            getLogList(pageNo, pageSize,name,url) {
                var _this = this
                axiosHttpUtil(serveceApi.menuList,{
                    pageNo:pageNo,
                    pageSize:pageSize
                }).then((res)=>{
                    if(res && res.data && res.data.status == 0){
                        setTimeout(function(){
                            _this.listLoading=false
                        },1000)
                        _this.tableData = res.data.data
                        _this.total = res.data.totalCount
                        _this.listLoading=true

                        this.$message.success( '查询成功'  );
                    }else {
                        this.$message.error(   '查询error'   );
                    }

                },(error)=>{
                    console.log(error)
                })

            },
            //点击页码事件
            handleCurrentChange(pageNo) {
                this.getLogList(pageNo, this.pageSize)
            },
            //改变分页大小事件
            handleSizeChange(pageSize) {
                this.getLogList(this.pageNo, pageSize)
            },
            //删除
            handleDel: function (index, row) {
                var _this=this
                _this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(function(){
                    //请用请求进行删除
                    _this.$message({
                        message: row.id+'删除成功',
                        type: 'success'
                    });
                }).catch(function(){
                    _this.$message({
                        message: row.id+'删除失败',
                        type: 'error'
                    });
                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {

            },
            //显示新增界面
            handleAdd: function () {
                this.addFormVisible=true
            },
            //编辑
            editSubmit: function () {

            },
            //新增
            addSubmit: function () {
                //表单校验
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        let params = Object.assign({}, this.addForm);
                        console.log(params)
                        //发送请求提交数据
                    } else {
                        this.$message.error('error submit!!')
                        return false
                    }
                })
            },
            //新增模态框关闭
            addFromClose:function(){
                this.addFormVisible=false
                this.$refs['addForm'].resetFields()
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var _this=this
                _this.$confirm('确认删除所选?', '警告', {
                    type: 'warning'
                }).then(function(){
                    //请用请求进行删除
                    _this.$message({
                        message: '删除成功',
                        type: 'success'
                    });
                }).catch(function(){
                    _this.$message({
                        message: row.id+'删除失败',
                        type: 'error'
                    });
                });
            },
            //搜索菜单
            searchMenu:function(){
                this.getLogList(this.pageNo,this.pageSize,this.filters.name,this.filters.url)
            },
            nameFlagClick:function () {

            }
        }

    }

</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }

    .del-dialog-cnt {
        font-size: 16px;
        text-align: center
    }
</style>
